/**
 * Created by leon on 2017/6/20.
 */

import {Injectable} from '@angular/core';
import { Headers, Http } from '@angular/http';
import { XAuth } from './auth/x-auth';
import { NavigationExtras } from '@angular/router';
import { environment } from '../../environments/environment';

@Injectable()
export class DataService {
    private host = environment.host;
    public token: string;
    public navExtras: NavigationExtras;


    private _xAuth: String;
    constructor(private http: Http) { }

    get xAuth(): string {
        return '';
    }

    set xAuth(xAuth: string) {
         this._xAuth = xAuth;
        // this._getHeaders.set('X-AuthorizationToken', xAuth);
        // this._postHeaders.set('X-AuthorizationToken', xAuth);
    }
    getXAuth(): Promise<string> {

             const url = `${ this.host }/api/auth/login`;
            // return null ;
            return this.http.get(url)
                 .toPromise()
                 .then(response => response.json() as XAuth)
                 .then(response => this.xAuth = response.authorizationTokenValue)
                 .catch(this.handleError);

    }

    private handleError(error: any): Promise<any> {
        console.error('An error occurred', error); // for demo purposes only
        return Promise.reject(error.message || error);
    }
}
